import createSmyooApi from './smyoo-api';

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

export const setVoiceSystem = async (status, text) => {
  const _classSmyooApi = createSmyooApi({
    mcuName: 'SYN_085DB4',
    mcuid: 'E19421EB9548E43D62DA10607778244E',
    status,
    text
  });
  await _classSmyooApi.immediateApi();
  await _classSmyooApi.apiSpeakerPlayText();
}

export const setBallSystem = async (mcuName, mcuid, status) => {
  const _classSmyooApi = createSmyooApi({
    mcuName,
    mcuid,
    status
  });
  await _classSmyooApi.immediateApi();
  await _classSmyooApi.apiSetDevicedata();
}

export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  // ${[hour, minute, second].map(formatNumber).join(':')}
  return `${[year, month, day].map(formatNumber).join('-')}`
}

export const sum = (list) => {
  return list.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
}


export function timestampToTime(value, type = 0) {
  var time = new Date(value);
  var year = time.getFullYear();
  var month = time.getMonth() + 1;
  var date = time.getDate();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  month = month < 10 ? "0" + month : month;
  date = date < 10 ? "0" + date : date;
  hour = hour < 10 ? "0" + hour : hour;
  minute = minute < 10 ? "0" + minute : minute;
  second = second < 10 ? "0" + second : second;
  var arr = [
    year + "-" + month + "-" + date,
    year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second,
    year + "年" + month + "月" + date,
    year + "年" + month + "月" + date + " " + hour + ":" + minute + ":" + second,
    hour + ":" + minute + ":" + second,
    {
      hour,
      minute
    },
  ]
  return arr[type];
}

export function parseData(time) {
  let mm = time / 1000 | 0;
  let m = mm / 60 % 60 | 0;
  let h = mm / 3600 | 0;
  console.log(h, m)
  return [String(h).padStart(2, '0'), String(m).padStart(2, '0')]
}

export function getHSTime(time) {
  const t = time / 1000;
  if (t <= 0) {
    return 0
  }
  const hours = Math.floor(t / 3600);
  const minutes = Math.floor((t % 3600) / 60);

  // 返回格式化的时间字符串  
  return `${hours}小时${minutes}分钟`;
}

export function generateUniqueString(length) {
  let result = '';
  let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let charactersLength = characters.length;
  for (let i = 0; i < length; i++) {
    let randomIndex = Math.floor(Math.random() * charactersLength);
    result += characters.charAt(randomIndex);
  }
  return result;
}

export const getParameterByName = (name, url) => {
  const params = url.split(`${name}=`)[1];
  return params ? params.split('&')[0] : '';
}

export const punctuationRegex = /^[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]+|[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]+$/g;


/**
 * 获取指定的URL参数值
 * URL:  https://xxx/s?mcuName=SYN_04E311&mcuid=C9B0A4CCFB7D4F420B4B7A524CAD33FC
 * 参数：paramName URL参数
 * 调用方法:getQueryString(URL)
 * 返回值:{mcuName: SYN_04E311, mcuid: C9B0A4CCFB7D4F420B4B7A524CAD33FC}
 */
export const getQueryString = (URL) => {
  let num = URL.indexOf("?")
  URL = URL.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
  const arr = URL.split("&"); //各个参数放到数组里
  const options = {}
  for (var i = 0; i < arr.length; i++) {
    num = arr[i].indexOf("=");
    if (num > 0) {
      options[arr[i].substring(0, num)] = arr[i].substr(num + 1);
    }
  }
  return options
}

export const stageList = [{
  id: 1,
  name: '会员开台',
  desc: '会员余额大于押金直接开台',
  used: true
},
{
  id: 2,
  name: '自助开台',
  desc: '押金开台，结束后自动退回',
  used: true
},
{
  id: 3,
  name: '抖音团购开台',
  desc: '抖音团购券复制券码开台',
  used: true
},
{
  id: 4,
  name: '美团/点评开台',
  desc: '添加店长微信兑换开台',
  used: true
},
{
  id: 5,
  name: '定时开台',
  desc: '设定开台时长，打球免押金',
  used: true
}
];